package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.aps.bean.Bom;
import com.efast.dmom.aps.bean.Part;
import com.efast.dmom.aps.dao.PartDao;
import com.efast.dmom.aps.entity.PartEntity;
import com.efast.dmom.aps.util.SpringWebUtils;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class PartHandler{
	@Autowired
	private PartDao partDao;
	@Autowired
	private BomHandler bomHandler;
	
	public void save(Part obj) {
		
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		
		
		partDao.save(obj.getInnerData());
	}

	public void update(Part obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		partDao.update(obj.getInnerData());
	}

	public void delete(Part obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);		
		partDao.delete(obj.getInnerData());
	}

	public Part getById(long id) {
		Part part = new Part();
		part.setInnerData(partDao.getById(id));
		return part;
	}

	public Part getByName(String name) {
		Part part = new Part();
		part.setInnerData(partDao.getByName(name));
		return part;
	}
	
	public Part getPartByPartNo(String partNo,String revision) {
		Part part = new Part();
		PartEntity partByPartNo = partDao.getPartByPartNo(partNo, revision);
		if(partByPartNo!=null) {
			part.setInnerData(partByPartNo);
			String bomName = partByPartNo.getBomName();
			String bomRevision = partByPartNo.getBomRevision();
			if(bomName!=null && !"".equals(bomName) && bomRevision!=null && !"".equals(bomRevision)) {
				Bom bom = bomHandler.getByNameAndRevision(bomName, bomRevision);
				part.setBom(bom);
			}
		}
		return part;
	}
	
	public Part getPartByPartNo(String partNo) {
		Part part = new Part();
		part.setInnerData(partDao.getByName(partNo));
		return part;
		
	}

	public List<Part> getAllPart() {
		List<PartEntity> entityList = partDao.getAllPart();
		List<Part> partList = new ArrayList<Part>();
		if (entityList != null && entityList.size() > 0) {
			for(PartEntity entityData:entityList) {
				Part part = new Part();
				part.setInnerData(entityData);
				partList.add(part);
			}
			return partList;
		}
		return null;
	}

	public List<Part> getPartByBomNameAndRevision(String bomName, String bomRevision) {
		List<PartEntity> entityList = partDao.getPartByBomNameAndRevision(bomName, bomRevision);
		List<Part> partList = new ArrayList<Part>();
		if (entityList != null && entityList.size() > 0) {
			for(PartEntity entityData:entityList) {
				Part part = new Part();
				part.setInnerData(entityData);
				partList.add(part);
			}
			return partList;
		}
		return null;
	}

	public List<PartEntity> getPartByPartNoAndRevision(Part part) {
		List<PartEntity> entityList = partDao.getPartByPartNoAndRevision(part.getInnerData().getPartNo(), part.getInnerData().getPartRevision(),part.getStart(),part.getLength());
		if (entityList != null && entityList.size() > 0) {
			return entityList;
		}
		return null;
	}
}
